#include "stc15w4k32s4.h"
#include "intrins.h"
//RXD接31
//TXD接30
bit  flag_REC=0; 
bit  flag    =0; 
sbit M1A = P0 ^ 0;				   //左边
sbit M1B = P0 ^ 1;				  //左边
sbit M2A = P0 ^ 2;				  //右边
sbit M2B = P0 ^ 3;				  //右边
sbit Y = P0 ^ 4;				 //语音播放
sbit L = P0 ^ 5;				//PWM信号输出
unsigned char  i=0;
unsigned char  cat=0;			//单片机接收数据中间变量
unsigned char  buff[5]=0;		//接收缓冲字节
unsigned char tmp;				//蓝牙接受判断变量
unsigned char count;			//0.5ms次数标识
char dat=1;
void ConfigUART();				//串口初始化
void send(unsigned char a);		//数据发送
void delay_ms(int ms);			//延时
void ctrl();					//数据处理
void tingzhi();					//小车停止	
void qianjin();					//小车前进	
void zuozhuan();				//小车左转	
void youzhuan();				//小车右转	
void luyin();					//录音
void yuyin();					//语音播放
void qiqiu();					//释放气球

void main()
{
	P0M0=0X00;
	P0M1=0X00;
	jd = 1;
	count = 0;
	ConfigUART();		//串口初始化  
	while(1)
		{
				 RI = 0;		
				 tmp = SBUF;
				 ctrl();
		}
}

void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=0;j<1146;j++);
}

void ConfigUART()
{	
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x50;		//8位数据,可变波特率
	AUXR &= 0xBF;		//定时器1时钟为Fosc/12,即12T
	AUXR &= 0xFE;		//串口1选择定时器1为波特率发生器
	TMOD &= 0x0F;		//清除定时器1模式位
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式
	TL1 = 0xFD;		//设定定时初值
	TH1 = 0xFD;		//设定定时器重装值
	ET1 = 0;		     //禁止定时器1中断
	TR1 = 1;		     //启动定时器1
 
}

void send(unsigned char a)      //单字节数据发送
{                                                              
	TI = 0;
	SBUF = a;
	while (TI == 0);
	TI = 0;
}

void tingzhi()  //电机初始化
 {
   M1A=0;                                
   M1B=0;                                
   M2A=0;                                  
   M2B=0;
	 Y=0;
	 L=0;
 }

void qianjin()
{
	 M1A=1;                                   
   M1B=0;                                   
   M2A=1;                                   
   M2B=0;
}
void houtui()
{
	 M1A=0;                                   
   M1B=1;                                   
   M2A=0;                                   
   M2B=1;
}
void zuozhuan()
{
   M1A=0;                                   
   M1B=1;                                   
   M2A=1;                                   
   M2B=0;
}
void youzhuan()
{
	 M1A=1;                                   
   M1B=0;                                   
   M2A=0;                                   
   M2B=1;
}
void qiqiu()
{	
		L=1;
}
void yuyin()
{
	 Y=1;
}


void ctrl()
{
	switch(tmp)
	{
		case 'W': delay_ms(50);send(tmp);P50=0;qianjin();break; // P10=1,P12=1;P11=0,P13=0;
		case 'X': delay_ms(50);send(tmp);P50=1;houtui();break;  //P01=1,P03=1;P00=0,P02=0
		case 'D': delay_ms(50);send(tmp);zuozhuan();break; //P02=1;P03=0
		case 'A': delay_ms(50);send(tmp);youzhuan();break; //P00=1;P01=0
		case 'E': delay_ms(50);send(tmp);yuyin();break;  //语音播报 
		case 'Q': delay_ms(50);send(tmp);qiqiu();break;  //放气球
		case 'S': delay_ms(50);send(tmp);tingzhi();break;
		//case 'k':send(tmp); duoji_2(); break;
		default:
		P21 = 1 ; send(tmp);
		}
}


void sint() interrupt 4	  //中断接收3个字节
{ 
    if(RI)	                 //是否接收中断
    {
       RI=0;
       cat=SBUF;
       if(cat=='O'&&(i==0)) //接收数据第一帧
         {
            buff[i]=cat;
            flag=1;        //开始接收数据
         }
       else
      if(flag==1)
     {
      i++;
      buff[i]=cat;
      if(i>=2)
      {i=0;flag=0;flag_REC=1;}  // 停止接收
     }
	 }
